//
//  HomePageViewController.m
//  NewProject
//
//  Created by apple on 2018/5/31.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "HomePageViewController.h"
#import "BannerTableViewCell.h"
#import "ToolTableViewCell.h"
#import "LotteryTableViewCell.h"
#import "HomePageManager.h"
#import "DiscoverDetailController.h"
@interface HomePageViewController ()

@property (weak, nonatomic) IBOutlet UITableView *homeTableView;

@property (nonatomic,retain)NSMutableArray* listArray;

@end

@implementation HomePageViewController{
    NSDictionary* lotIDImageDic;
    NSMutableArray* newList;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"亦达彩票";
    
    lotIDImageDic = @{
                      @"9":@"ks",
                      @"5":@"福彩3D",
                      @"6":@"排列三",
                      @"7":@"11x5",
                      @"3":@"双色球",
                      @"4":@"大乐透"
                      };
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(nextAction) name:@"kNextAction" object:nil];
    
    [self.homeTableView registerNib:[UINib nibWithNibName:@"BannerTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"BannerTableViewCell"];
    [self.homeTableView registerNib:[UINib nibWithNibName:@"ToolTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"ToolTableViewCell"];
    [self.homeTableView registerNib:[UINib nibWithNibName:@"LotteryTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"LotteryTableViewCell"];
    
    
    
    
    self.listArray = [NSMutableArray arrayWithCapacity:0];
    __weak typeof(self)weakSelf = self;
    [[LotteryManager sharedManager] getLotteryListActionWithCallBack:^(BOOL load, NSDictionary *resObj) {
        if (load) {
            if ([resObj[@"code"] integerValue]==0) {
                [weakSelf.listArray addObjectsFromArray:resObj[@"data"]];
                [weakSelf loadAction];
            }
        }
    }];
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




-(void)loadAction{
    
    newList = [[NSMutableArray alloc] initWithCapacity:0];
    for (int i=0; i<[self.listArray count]; i++) {
        NSDictionary* dic = self.listArray[i];
        NSString* checkKey = [dic[@"LotteryID"] stringValue];
        if ([lotIDImageDic[checkKey] length]>0) {
            [newList addObject:dic];
        }
    }
    
    [self.homeTableView reloadData];
}

-(void)nextAction{
    NSLog(@"%ld",[HomePageManager sharedManager].actionIndex);
    
//    [LotteryManager sharedManager].currentLotteryID
//    [LotteryManager sharedManager].currentLotteryName
    NSString* storyName = @"";
    NSString* viewConID = @"";
    switch ([HomePageManager sharedManager].actionIndex) {
        case 1:
            storyName = @"TrendForLot";
            viewConID = @"TrendViewController";
            break;
        case 2:
            storyName =  @"MissedNumber";
            viewConID =  @"MissedNumViewController";
            break;
        case 3:
        {
            storyName =  @"DiscoverStoryboard";
            viewConID =  @"DiscoverDetailController";
            UIStoryboard* storyBoard = [UIStoryboard storyboardWithName:storyName bundle:[NSBundle mainBundle]];
            DiscoverDetailController *detailVC = [storyBoard instantiateViewControllerWithIdentifier:viewConID];
            detailVC.titleStr = @"专家预测";
            detailVC.type = [LotteryManager sharedManager].currentLotteryID;
            [self.navigationController pushViewController:detailVC animated:YES];
            return;}
        case 4:{
            storyName =  @"DiscoverStoryboard";
            viewConID =  @"DiscoverDetailController";
            UIStoryboard* storyBoard = [UIStoryboard storyboardWithName:storyName bundle:[NSBundle mainBundle]];
            DiscoverDetailController *detailVC = [storyBoard instantiateViewControllerWithIdentifier:viewConID];
            detailVC.titleStr = @"技巧攻略";
            detailVC.type = [LotteryManager sharedManager].currentLotteryID;
            [self.navigationController pushViewController:detailVC animated:YES];
            return;}

        default:
            break;
    }
    
    UIStoryboard* storyBoard = [UIStoryboard storyboardWithName:storyName bundle:[NSBundle mainBundle]];
    [self.navigationController pushViewController:[storyBoard instantiateViewControllerWithIdentifier:viewConID] animated:YES];
}


#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [newList count]+2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row==0) {
        BannerTableViewCell* bCell = [tableView dequeueReusableCellWithIdentifier:@"BannerTableViewCell"];
        bCell.tapIndex = ^(NSInteger index) {
            NSLog(@"%ld",index);
        };
        return bCell;
    }
    if (indexPath.row==1) {
        ToolTableViewCell* tCell = [tableView dequeueReusableCellWithIdentifier:@"ToolTableViewCell"];
        tCell.actionBlock = ^(NSInteger buttonIndex) {
            switch (buttonIndex) {
                case 1:{
                    UIViewController* acViewCon = [[UIStoryboard storyboardWithName:@"SelectedNumber" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"LLForSelectedTableViewController"];
                    [self.navigationController pushViewController:acViewCon animated:YES];
                }
                    break;
                case 2:{
                    UIViewController* acViewCon = [[UIStoryboard storyboardWithName:@"Recommend" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"RecommendViewController"];
                    [self.navigationController pushViewController:acViewCon animated:YES];
                }
                    break;
                case 3:{
                    UIViewController* acViewCon = [[UIStoryboard storyboardWithName:@"LuckyNumber" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"LuckyNumberViewController"];
                    [self.navigationController pushViewController:acViewCon animated:YES];
                }
                    break;
                case 4:{
                    UIViewController* acViewCon = [[UIStoryboard storyboardWithName:@"SchoolStoryboard" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"SchoolController"];
                    [self.navigationController pushViewController:acViewCon animated:YES];
                }
                    break;
                default:
                    break;
            }
        };
        return tCell;
    }
    
    NSDictionary* dic = newList[indexPath.row-2];
    NSString* checkKey = [dic[@"LotteryID"] stringValue];
    
    LotteryTableViewCell* lCell = [tableView dequeueReusableCellWithIdentifier:@"LotteryTableViewCell"];
    lCell.titleImageView.image = [UIImage imageNamed:lotIDImageDic[checkKey]];
    [lCell loadCellWith:dic];
    return lCell;
}
//{
//    EndTime = "2018/06/01";
//    HTeam = "";
//    ID = 953597;
//    IssueName = 20180601033;
//    LotteryID = 9;
//    LotteryName = "\U5feb3";
//    QuickFlag = 2;
//    RValue = "";
//    Rz = "1 4 5";
//    VTeam = "";
//    WinNumber = "1 4 5";
//}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat height = 130;
    if (indexPath.row==0) {
        height = 126;
    }
    if (indexPath.row==1) {
        height = 90;
    }
    return height;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
